home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / V15N04.ZIP / WARPCA.ZIP / WCABSRC.ZIP / FILENTRY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  3.0 KB  |  137 lines

  1. // FILENTRY.CPP -- Simple class to manage files in listboxes (implementation).
  2. #define INCL_WINSHELLDATA
  3. #include <os2.h>
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <cstring.h>
  7. #include "resource.h"
  8. #include "filentry.h"
  9.  
  10. int FILEENTRY::nSortMethod = SORT_BY_NAME;
  11.  
  12. FILEENTRY::FILEENTRY(struct ffblk& FileInfo, BOOL bIsDir, int nMySortMethod)
  13. {
  14.     int nFound;
  15.  
  16.     // Get entry from the contents of this struct ffblk.
  17.     nSortMethod = nMySortMethod;  // Note this is a static member. One value per all objects of this class!
  18.                                             // Change this and we change the way our entries are sorted.
  19.  
  20.     lFileSize = FileInfo.ff_fsize;
  21.     ulFileAttrib = FileInfo.ff_attrib;
  22.     uFileTime = FileInfo.ff_ftime;
  23.     uFileDate = FileInfo.ff_fdate;
  24.  
  25.     strcpy(szFileName, FileInfo.ff_name);
  26.  
  27.     for(LPSTR p = szFileName; *p != '\0' && *p != '.'; p++);
  28.  
  29.     if(*p == '.')
  30.         strcpy(szExt, p + 1);  // Save extension.
  31.     else
  32.         strcpy(szExt, "");  // (No extension.)
  33.  
  34.     if(bIsDir)
  35.     {
  36.         nType = 1;
  37.         iImage = ID_FOLDER;
  38.     }
  39.     else
  40.     {
  41.         if(strcmpi(szExt, "EXE") == 0)
  42.         {
  43.             nType = 2;
  44.             iImage = ID_EXE;
  45.         }
  46.         else if(strcmpi(szExt, "CMD") == 0)
  47.         {
  48.             nType = 3;
  49.             iImage = ID_CMD;
  50.         }
  51.         else if(strcmpi(szExt, "BAT") == 0)
  52.         {
  53.             nType = 4;
  54.             iImage = ID_BAT;
  55.         }
  56.         else if(strcmpi(szExt, "COM") == 0)
  57.         {
  58.             nType = 5;
  59.             iImage = ID_COM;
  60.         }
  61.         else
  62.         {
  63.             // Then it's some kind of document...
  64.             nType = 6;
  65.             // Find association, if any.
  66.             iImage = ID_DOCUMENT;
  67.         }
  68.     }
  69. }
  70.  
  71. void FILEENTRY::UpperCase(char *pszOut, char *pszIn, int nMaxLen)
  72. {
  73.     if(!pszOut || !pszIn)
  74.         return;
  75.  
  76.     for(int i = 0; i < strlen(pszIn) && i < nMaxLen; i++)
  77.         pszOut[i] = toupper(pszIn[i]);
  78.  
  79.     if(i < nMaxLen)
  80.         pszOut[i] = '\0';
  81. }
  82.  
  83. int FILEENTRY::operator<(const FILEENTRY& CompareItem)
  84. {
  85.     int bIsLessThan = FALSE;
  86.     int rc;
  87.  
  88.     char szTemp1[257];
  89.     char szTemp2[257];
  90.  
  91.     // Folders always appear before files!!!
  92.     if(nType == 1 && CompareItem.nType != 1)
  93.         return TRUE;
  94.     else if(nType != 1 && CompareItem.nType == 1)
  95.         return FALSE;
  96.  
  97.     // Otherwise, sort within folders or files
  98.     switch(nSortMethod)
  99.     {
  100.         case SORT_BY_NAME:
  101.                 UpperCase(szTemp1, szFileName, 256);
  102.                 UpperCase(szTemp2, (char *)CompareItem.szFileName, 256);
  103.                 rc = strcmpi(szTemp1, szTemp2);
  104.                 if(rc < 0)
  105.                     bIsLessThan = TRUE;
  106.                 break;
  107.         case SORT_BY_DATE:
  108.                 if(uFileDate == CompareItem.uFileDate)
  109.                     // Latest files first!
  110.                     bIsLessThan = uFileTime > CompareItem.uFileTime;
  111.                 else
  112.                     bIsLessThan = uFileDate > CompareItem.uFileDate;
  113.                 break;
  114.         case SORT_BY_EXT:
  115.                 rc = strcmpi(szExt, CompareItem.szExt);
  116.                 if(rc < 0)
  117.                     bIsLessThan = TRUE;
  118.                 break;
  119.         case SORT_BY_SIZE:
  120.                 // Largest files first!
  121.                 bIsLessThan = lFileSize > CompareItem.lFileSize;
  122.                 break;
  123.     }
  124.     return bIsLessThan;
  125. }
  126.  
  127. int FILEENTRY::operator==(const FILEENTRY& CompareItem)
  128. {
  129.     // Assumes equivalent filenames are equivalent objects.
  130.     if(nType == CompareItem.nType && strcmpi(szFileName, CompareItem.szFileName) == 0)
  131.         return TRUE;
  132.     else
  133.         return FALSE;
  134. }
  135.  
  136.  
  137.